<template>
    <div class="Mynews">
        <div class="Mynews-head">
            <span>通知</span>
            <ul>
                <li v-for="(item,index) in navList" :key="index" @click="SelectIndxe(index)" :class="navListIndex == index?'navslecet':''">{{item}}</li>
            </ul>
        </div>
        <div v-if="navListIndex == 0" class="Mynews-content">
            <ul>
                <li v-for="(item,index) in MessageList" :key="index">
                    <img :src="item.cover"/>
                    <div>
                        <label>
                            <span>{{item.nick_name}}</span>
                            <span>{{item.content}}</span>
                        </label>
                        <label>
                            <span>{{item.classroom}}</span>
                            <span>{{item.timer}}</span>
                        </label>
                    </div>
                    <div class="viewsmessages-content-right">
                        <label v-if="item.untreated" class="untreated">
                            <span>同意</span>
                            <span>拒绝</span>
                        </label>
                        <label v-else>
                            <span v-if="item.agreedto">已同意</span>
                            <span v-else>已拒绝</span>
                        </label>
                    </div>
                </li>
            </ul>
        </div>
        <div v-else-if="navListIndex == 1" class="Mynews-content">
            <span class="Allmarkedread">全部标为已读</span>
            <ul>
                <li v-for="(item,index) in MyNewsList" :key="index">
                    <img :src="item.userProfile.avatar"/>
                    <div>
                        <label>
                            <span>{{item.userProfile.nick}}</span>
                            <span>{{item.lastMessage.lastTime* 1000 | formatDate}}</span>
                        </label>
                        <label>
                            <span>{{item.lastMessage.messageForShow}}</span>
                            <!-- <span>{{item.timer}}</span> -->
                        </label>
                    </div>
                    <div class="viewsmessages-content-right">
                        <label class="untreated" @click="Tochat(item)">
                            <img src="/images/组 11@2x.png"/>
                        </label>
                    </div>
                </li>
                <li v-for="(itemGroup,Groupindex) in GroupNewList" :key="Groupindex">
                    <img :src="itemGroup.groupProfile.avatar"/>
                    <div>
                        <label>
                            <span>{{itemGroup.groupProfile.name}}</span>
                            <span>{{itemGroup.lastMessage.lastTime* 1000 | formatDate}}</span>
                        </label>
                        <label>
                            <span>{{itemGroup.lastMessage.messageForShow}}</span>
                            <!-- <span>{{item.timer}}</span> -->
                        </label>
                    </div>
                    <div class="viewsmessages-content-right">
                        <label class="untreated" @click="Tochat(itemGroup)">
                            <img src="/images/组 11@2x.png"/>
                        </label>
                    </div>
                </li>
            </ul>
        </div>
        <div v-else-if="navListIndex == 2" class="Mynews-content">
            <ul>
                <li v-for="(item,index) in MessageList" :key="index">
                    <img :src="item.cover"/>
                    <div>
                        <label>
                            <span>{{item.nick_name}}</span>
                            <span>{{item.content}}</span>
                        </label>
                        <label>
                            <span>{{item.classroom}}</span>
                            <span>{{item.timer}}</span>
                        </label>
                    </div>
                    <div class="viewsmessages-content-right">
                        <label class="untreated">
                            <span>评论</span>
                            <img src="/images/组 11@2x.png"/>
                        </label>
                    </div>
                </li>
            </ul>
        </div>
    </div>
</template>
<script>
import { formatDate } from "../js/them";
import TIM from 'tim-js-sdk';
let options = {
  SDKAppID: 1400255047 // 接入时需要将0替换为您的即时通信 IM 应用的 SDKAppID
};
let tim = TIM.create(options); // SDK 实例通常用 tim 表示
export default {
    name:'Mynews',
    data(){
        return{
            navList:['我的消息','消息','待评论'],
            navListIndex:'',
            MessageList:[
                {
                    cover:'/images/图层 9.png',
                    nick_name:'后浪',
                    content:"西班牙语",
                    classroom:'话题/学习教育',
                    timer:'2020-05-18  09:00-09:30',
                    untreated:false,
                    agreedto:false
                },
                {
                    cover:'/images/图层 9.png',
                    nick_name:'后浪',
                    content:"阿拉伯语",
                    classroom:'话题/学习教育',
                    timer:'2020-05-18  09:00-09:30',
                    untreated:true,
                    agreedto:false
                },
                {
                    cover:'/images/图层 9.png',
                    nick_name:'后浪',
                    content:"中文",
                    classroom:'话题/学习教育',
                    timer:'2020-05-18  09:00-09:30',
                    untreated:false,
                    agreedto:true
                }
            ],
            MyNewsList:[],
            GroupNewList:[],
        }
    },
    filters: {
        formatDate(time) {
            var date = new Date(time);
        return formatDate(date, "yyyy-MM-dd hh:mm");
        }
    },

    created:function(){
        this.GetMessage()
        this.ISLogo()
    },
    methods:{
        SelectIndxe(e){
            this.navListIndex = e
        },
        Tochat(e){
            console.log(e)
            if(e.type == "GROUP"){
                this.$router.push({path:'/views/Learningcenter/Chat',query: {toid: e.toAccount,name:e.groupProfile.name,conversationID:e.conversationID,avatar:e.groupProfile.avatar}})
            }else if(e.type == "C2C"){
                this.$router.push({path:'/views/Learningcenter/Chat',query: {toid: e.toAccount,name:e.userProfile.nick,conversationID:e.conversationID,avatar:e.userProfile.avatar}})
            }
            
        },
        ISLogo(){
            var _this = this
            tim.on(TIM.EVENT.CONVERSATION_LIST_UPDATED, function(event) {
                console.log(event,'9099')
                _this.GroupNewList = []
                _this.MyNewsList = []
                let promise = tim.getConversationList();
                promise.then(function(imResponse) {
                    imResponse.data.conversationList.forEach(e => {
                        if(e.type == "C2C"){
                            _this.MyNewsList.push(e)
                        }else if(e.type == "GROUP"){
                            _this.GroupNewList.push(e)
                        }
                    });
                    console.log(imResponse.data.conversationList,'88888')
                }).catch(function(imError) {
                    console.warn('getConversationList error:', imError); // 获取会话列表失败的相关信息
                });;
                // 收到会话列表更新通知，可通过遍历 event.data 获取会话列表数据并渲染到页面
                // event.name - TIM.EVENT.CONVERSATION_LIST_UPDATED
                // event.data - 存储 Conversation 对象的数组 - [Conversation]
            });
        },
        GetMessage(){
            // 将某会话下所有未读消息已读上报
            var _this = this
            let promise = tim.getConversationList();
            promise.then(function(imResponse) {
                imResponse.data.conversationList.forEach(e => {
                    if(e.type == "C2C"){
                        _this.MyNewsList.push(e)
                    }else if(e.type == "GROUP"){
                        _this.GroupNewList.push(e)
                    }
                });
                console.log(imResponse.data.conversationList,'88888')
            }).catch(function(imError) {
                console.warn('getConversationList error:', imError); // 获取会话列表失败的相关信息
            });;
        }
    }
}
</script>
<style lang="less" scoped>
    .Mynews{
        position: relative;
        width: 100%;
        background: #ffffff;
    }
    .Mynews-head{
        position: relative;
        width: 90%;
        margin: 0 auto;
        height: 100px;
        display: flex;
        align-items: center;
        border-bottom: 1px solid #2DC7C8;
        span{
            width: 20%;
            height: 30px;
            line-height: 30px;
            border-left: 14px solid #2DC7C8;
            padding-left: 2%;
            color: #2DC7C8;
            font-weight: bold;
            font-size: 24px;
        }
    }
    .Mynews-head{
        ul{
            list-style: none;
            display: flex;
            padding: 0;
            width: 80%;
            justify-content: flex-end;
            li{
                width: 13%;
                height: 30px;
                text-align: center;
                cursor: pointer;
            }
        }
    }
    .navslecet{
        color: #47CECF;
        border-bottom: 4px solid #47CECF;
    }
    /*内容样式*/
    .Mynews-content{
        position: relative;
        width: 90%;
        margin: 0 auto;
        margin-top: 30px;
        margin-bottom: 100px;
        ul{
            position: relative;
            width: 90%;
            margin: 0 auto;
            display: flex;
            padding: 0;
            flex-wrap: wrap;
            list-style: none;
            li{
                width: 100%;
                height: 100px;
                display: flex;
                align-items: center;
                // justify-content: center;
                border-bottom: 1px solid #f1f1f1;
                img{
                    width: auto;
                    height: auto;
                    max-width: 70%;
                    max-height: 70%;
                    border-radius: 100px;
                }
                div{
                    width: 50%;
                    height: 80px;
                    &:first-child{
                        margin-left: 4%;
                    }
                    label{
                        display: flex;
                        flex-wrap: wrap;
                        height: 40px;
                        line-height: 40px;
                        &:first-child{
                            span{
                                // color: #47CECF;
                                &:first-child{
                                    font-size: 14px;
                                    color: #353535;
                                    font-weight: bold;
                                }
                            }
                        }
                        &:last-child{
                          height: 30px;
                          line-height: 30px;  
                        }
                        span{
                            color: #C2C2C2;
                            font-size: 12px;
                            margin-left: 4%;
                        }
                    }
                }
            }
        }
    }
    .viewsmessages-content-right{
        width: 50%;
        height: 80px;
        text-align: right;
        line-height: 80px;
        label{
            display: flex;
            justify-content: flex-end;
            height: 80px!important;
            text-align: right;
            align-items: center;
            cursor: pointer;
            span{
                color: #C2C2C2;
                font-size: 16px!important;
                font-weight: normal!important;
            }
        }
    }
    .untreated>span{
        width: 13%;
        height: 30px;
        line-height: 30px;
        text-align: center;
        color: #47CECF!important;
    }
    .Allmarkedread{
        display: flex;
        width: 90%;
        margin:  0 auto;
        justify-content: flex-end;
        color: #2DC7C8;
        font-size: 14px;
        cursor: pointer;
    }
</style>